package main

import (
	"io"
	"log"
	"net"
	"os"
	"sync"

	"unvarnished_transmission/client/config"
)

var (
	conf = config.Config()
)

func main() {
	go func() {
		for {
			connToServer1, err := net.Dial("tcp", conf.Server.TunnelAddr)
			if err != nil {
				log.Fatalf("Error connecting to server 1 on port 8084: %v", err)
			}

			connToGin, err := net.Dial("tcp", conf.Server.TargetAddr)
			if err != nil {
				log.Fatalf("Error connecting to Gin server: %v", err)
			}

			var wg sync.WaitGroup
			wg.Add(2)

			// 将从服务端1接收的流量转发给Gin服务器
			go func() {
				defer wg.Done()
				io.Copy(connToGin, connToServer1)
			}()

			// 将从Gin服务器接收的响应转发给服务端1
			go func() {
				defer wg.Done()
				io.Copy(connToServer1, connToGin)
			}()

			wg.Wait()

			connToServer1.Close()
			connToGin.Close()
		}
	}()

	signalChan := make(chan os.Signal, 1)

	<-signalChan
}
